﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TVXD23.Web.Models;
using TVXD23.Web.Models.ViewModel;

namespace TVXD23.Web.Controllers
{
    public class HRMController : Controller
    {

        private HRMRepositoty repository = new HRMRepositoty();
        //
        // GET: /HRM/
        #region Department
        
        public ActionResult Index()
        {
            List<Department> list  = repository.getAllDepartments();
            List<DepartmentsModel> listModel = new List<DepartmentsModel>();
            DepartmentsModel model = new DepartmentsModel();
            foreach (Department dep in list) {
                listModel.Add(new DepartmentsModel() { departmentId = dep.departmentId, Name = dep.Name});
            }
            model.listDepartment = listModel;
            return View("~/Views/HRM/Index.aspx", model);
        }

        //
        // GET: /HRM/Details/5

        public ActionResult DetailsDepart(int id)
        {
            
            Department dep = repository.getDepartmentById(id);
            DepartmentsModel model = new DepartmentsModel() { departmentId = dep.departmentId, Name = dep.Name, Description = dep.Description, DateCreate = (DateTime)dep.DateCreate };
            return View("~/Views/HRM/DetailsDepart.aspx", model);
        }

        //
        // GET: /HRM/Create

        public ActionResult CreateDepart()
        {
            DepartmentsModel model = new DepartmentsModel();
            return View("~/Views/HRM/CreateDepart.aspx", model);
        } 

        //
        // POST: /HRM/Create

        [HttpPost]       
        [ValidateInput(false)]
        public ActionResult CreateDepart(DepartmentsModel model)
        {
            try
            {
                var dep = new Department();
                dep.Name = model.Name;
                dep.Description = model.Description;
                dep.DateCreate = DateTime.Now;
                repository.addNewDepartment(dep);
                return RedirectToAction("Index", new { message = "Đã tạo xong department " + model.Name });
            }
            catch
            {
                return View("~/Views/HRM/Index.aspx");
            }          
        }
        
        //
        // GET: /HRM/Edit/5
 
        public ActionResult EditDepart(int id)
        {
            Department dep = repository.getDepartmentById(id);
            DepartmentsModel model = new DepartmentsModel() { departmentId = dep.departmentId, Name = dep.Name, Description = dep.Description, DateCreate = (DateTime)dep.DateCreate };
            return View("~/Views/HRM/EditDepart.aspx", model);
           
        }

        //
        // POST: /HRM/Edit/5

        [HttpPost]
        public ActionResult EditDepart(int id, DepartmentsModel dep)
        {
            try
            {
                dep.departmentId = id;
                repository.editDepartment(dep);               
              
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /HRM/Delete/5
 
        public ActionResult DeleteDepart(int id)
        {
            Department dep = repository.getDepartmentById(id);
            DepartmentsModel model = new DepartmentsModel() { departmentId = dep.departmentId, Name = dep.Name, Description = dep.Description, DateCreate = (DateTime)dep.DateCreate };
            return View("~/Views/HRM/DeleteDepart.aspx", model);
        }

        //
        // POST: /HRM/Delete/5

        [HttpPost]
        public ActionResult DeleteDepart(int id, FormCollection collection)
        {
            try
            {
                repository.deleteDepartment(id);
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        #endregion

        #region Employee manager
        public ActionResult ListEmp(int id)
        {
            List<Employee> list = repository.getListEmployeesByDeparment(id);
            List<EmployeesModel> listModel = new List<EmployeesModel>();
           
            foreach (Employee dep in list)
            {
                EmployeesModel emp = HRMRepositoty.convertDataToModel(dep);
                try { 
                    emp.NameDepartment = repository.getDepartmentById(emp.departmentId).Name;
                }
                catch(Exception ex){
                    emp.NameDepartment = "";
                }
                
                listModel.Add(emp);
            }

            return View("~/Views/HRM/ListEmp.aspx", listModel);
        }

        public ActionResult DetailsEmpl(int id)
        {

            Employee dep = repository.getEmployeeById(id);
            EmployeesModel emp = HRMRepositoty.convertDataToModel(dep);
            try
            {
                emp.NameDepartment = repository.getDepartmentById(emp.departmentId).Name;
            }
            catch (Exception ex)
            {
                emp.NameDepartment = "";
            }          

            return View("~/Views/HRM/DetailsEmpl.aspx", emp);
        }

        public ActionResult CreateEmpl()
        {
            EmployeesModel model = new EmployeesModel() { listDepartment = new List<DepartmentsModel>() };
            foreach (Department a in repository.getAllDepartments()) {
                if (a != null)
                {
                    model.listDepartment.Add(new DepartmentsModel() { departmentId = a.departmentId, Name = a.Name});
                }
            }
            return View("~/Views/HRM/CreateEmpl.aspx", model);
        }

        //
        // POST: /HRM/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreateEmpl(EmployeesModel model)
        {
            try
            {
                var dep = HRMRepositoty.convertModelToData(model);               
                repository.addNewEmployee(dep);
                return RedirectToAction("Index");
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }

        //
        // GET: /HRM/Edit/5

        public ActionResult EditEmpl(int id)
        {
            Employee dep = repository.getEmployeeById(id);
            EmployeesModel model = HRMRepositoty.convertDataToModel(dep);
            model.listDepartment = new List<DepartmentsModel>();
            foreach (Department a in repository.getAllDepartments())
            {
                if (a != null)
                {
                    model.listDepartment.Add(new DepartmentsModel() { departmentId = a.departmentId, Name = a.Name });
                }
            }
            return View("~/Views/HRM/EditEmpl.aspx", model);

        }

        //
        // POST: /HRM/Edit/5

        [HttpPost]
        public ActionResult EditEmpl(int id, EmployeesModel dep)
        {
            try
            {
                dep.employeeId = id;
                repository.editEmployee(dep);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /HRM/Delete/5

        public ActionResult DeleteEmpl(int id)
        {
            Employee dep = repository.getEmployeeById(id);
            EmployeesModel model = HRMRepositoty.convertDataToModel(dep);
            return View("~/Views/HRM/DeleteEmpl.aspx", model);
        }

        //
        // POST: /HRM/Delete/5

        [HttpPost]
        public ActionResult DeleteEmpl(int id, FormCollection collection)
        {
            try
            {
                repository.deleteEmployee(id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        #endregion
    }
}
